#include "KeyboardHandler.h"
#include <allegro.h>
#include <iostream>

using namespace std;

void KeyboardHandler::registerKey(char k, void(*funcPtr)(void)) {
	//mCallbacks.insert(pair<char,void(*)(void)>(k, funcPtr));
    mCallbacks[(int)k] = funcPtr;
}

int KeyboardHandler::handle(int k) {
    /*
     * This is a simple, but effective hack.
     * Execute the KEY:function mapping if it exists.
     */
    void(*fptr)(void) = mCallbacks[k>>8];
    if(fptr != NULL)
        fptr();
    else{
        cout << "Key: " << (k>>8) << " is unbound\n";
    }

    return 1;
}

/*int handleKeyboard(int key, ScreenManager screenMgr) {
    if(screenMgr->getActiveScreen() == gameScreen) { //if IN GAME -- better handled elsewhere.
        if(key >> 8 == KEY_SPACE) {
            cout << "drawing on gameScreen" << endl;
            //drawMgr->drawAll(gameScreen->draw());
        }

        else if(key >> 8 == KEY_ESC) {
            // TODO:activeScreen is increased for each screenswitch.
            // This might be a possible future bug. I.e. what happends
            // if activeScreen goes beyond MAX_INT? 
            screenMgr->setActiveScreen((activeScreen++)%2); 
            blit(screenMgr->getActiveScreen()->draw(),screen, 0,0,0,0,800,600);
        }
        
    }
    else if(screenMgr->getActiveScreen() == mainMenu) { //if IN MENU
        if(key >> 8 == KEY_ESC) {
            screenMgr->setActiveScreen((activeScreen++)%2); 
            blit(screenMgr->getActiveScreen()->draw(),screen, 0,0,0,0,800,600);
        }
    }

    return 1;
}*/
